package ui.activity.login;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import application.App;
import base.BaseBean;
import base.activity.BaseLayoutActivity;
import com.dispatchapp.andlei.R;
import com.maning.updatelibrary.InstallUtils;
import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;
import netWork.NetUtils;
import ui.activity.MainActivity;
import utils.SPUtils;
import widget.HiDialog;

import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * loaing加载页
 *
 * @author Andlei
 * @DATE 2019/1/17
 */
public class loadingActivity extends BaseLayoutActivity {
    private Timer timer;
    private NetUtils netUtil;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_loading;
    }

    @Override
    protected void findViews() {
        netUtil = new NetUtils(mActivity,netRequestCallBack);
    }

    @Override
    protected void formatViews() {
        timer = new Timer();
        if(!TextUtils.isEmpty(SPUtils.getInstance(loadingActivity.this).getString("token"))){
            netUtil.get("rider/user/update-token",BaseBean.class,false);
        }else {
            jumpTo(LoginActivity.class);
        }


    }

    @Override
    protected void formatData() {

    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onClick(View view) {

    }
    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void error(String action, Throwable e, Map tag) {
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if(!TextUtils.isEmpty(SPUtils.getInstance(loadingActivity.this).getString("token"))){
                        jumpTo(MainActivity.class);
                    }else {
                        jumpTo(LoginActivity.class);
                    }
                    finish();
                }
            },1000);
        }

        @Override
        public void success(String action, BaseBean t, Map tag) {
            if(t.getCode() == 200){
                SPUtils.getInstance(loadingActivity.this).put("token", t.getData().toString());
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if(!TextUtils.isEmpty(SPUtils.getInstance(loadingActivity.this).getString("token"))){
                            jumpTo(MainActivity.class);
                        }else {
                            jumpTo(LoginActivity.class);
                        }
                        finish();
                    }
                },1000);
            }else {
                jumpTo(LoginActivity.class);
            }

        }
    };

}
